import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
uni.getStorage({
	key: 'shopCart',
	success: res => {
		store.state.shopCart = res.data
	}
})
const store = new Vuex.Store({

	state: {
		//设定一个空数组来装购物车的相关数组
		// shopCart: [],
		// shopCart:sessionStorage.getItem("shopCart"),
		shopCart: null,
		// 装数量
		val: "",
		sname: null
	},
	mutations: {
		//加入购物车数组的方法
		addCar(state, val) {
			if (state.shopCart.length != 0) {
				let count = 0
				state.shopCart.map(item => {
					if (item.lid == val.lid) {
						item.count++
						count++
					}
				})
				if (count === 0) {
					state.shopCart.push(val)
					console.log(1)
				}
			} else {
				state.shopCart.push(val);
			}

			// state.shopCart.push(val);
		},
		//减少后替换到购物车数组的相关函数
		setShop(state, val) {
			state.shopCart = val
		},
		cgVal(state, val) {
			state.shopCart.count = val
		},
		//设置函数来控制购物车小标的数字
		setBadge(state, val) {
			state.sname = val
		}
	},
	actions: {}
})
export default store
